'use strict';

module.exports = app => {
  const mongoose = app.mongoose;
  const Schema = mongoose.Schema;

  const CacheDataSchema = new Schema({
    /**
     * 标题
     */
    data: { type: String, default: '' },
    /**
     * 时间
     */
    date: { type: Date },
  });

  /**
   * 删除过期的数据
   * @param { Number } overdueTime 过期时间
   */
  CacheDataSchema.statics.setData = async function(data) {
    const currentDate = new Date().getTime();
    return await new this({
      data,
      date: new Date()
    }).save();
  };

  // eslint-disable-next-line jsdoc/require-param
  /**
   * 查询短连接
   * * @param { Number } url 链接
   */
  CacheDataSchema.statics.getData = function(id) {
    return this.findOne({
      _id: mongoose.Types.ObjectId(id),
    });
  };

  return mongoose.model('CacheData', CacheDataSchema, 'cacheData');
};
